package com.andy.beef.login.fragment

import android.os.Bundle
import android.util.Log
import androidx.navigation.fragment.findNavController
import com.andy.beef.R
import com.andy.module.base.BaseVbFragment
import com.andy.module.progress.DownloadProgressListener
import com.andy.module.util.DownloadUtil
import com.andy.beef.databinding.FragmentHomeLoginBinding
import com.andy.beef.login.viewmodel.HomeLoginViewModel
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.LogUtils
import com.andy.ui.ext.clickNoRepeat
import okhttp3.*
import java.io.File
import java.io.IOException

/**
 * @creator:LuoFan
 * @date:2022/5/26
 * @description:登录首页
 */
class HomeLoginFragment : BaseVbFragment<HomeLoginViewModel, FragmentHomeLoginBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        BarUtils.setStatusBarLightMode(requireActivity(), false)
        mViewBind.llWxLogin.clickNoRepeat {
            test()
        }
        mViewBind.llPhoneLogin.clickNoRepeat {
            findNavController().navigate(R.id.toPhoneLogin)
        }
        mViewBind.tvAgreement.clickNoRepeat {

        }
        mViewBind.tvPrivacy.clickNoRepeat {

        }
    }

    private fun test() {
        val savePath = requireActivity().getExternalFilesDir("apk")!!.path
        val name = "testHello.apk"
        val url = "https://dnkj-world-farm-prd.oss-accelerate.aliyuncs.com/data/fc-bee/app/ChaseFlower_M.apk"
        DownloadUtil.download2(url, savePath, name, object : DownloadProgressListener {
            override fun onDownloadSuccess(file: File) {
                LogUtils.e("xxxxxxxxonDownloadSuccess" + file.path)
            }

            override fun onError(error: Throwable) {

            }

            override fun onChanged(current: Long, total: Long, percent: Double) {
                LogUtils.e(Thread.currentThread().name + "xxxxxxxxonChanged" + percent)
            }
        })
    }

    private fun upload() {

        val currentApkPath: String = requireActivity().getPackageResourcePath()
        val apkFile = File(currentApkPath)
        val url = "https://dnkj-world-farm-prd.oss-accelerate.aliyuncs.com/data/fc-bee/app/ChaseFlower_M.apk"
        val okHttpClient = OkHttpClient()
        val builder = Request.Builder()
        builder.url(url)
        val bodyBuilder = MultipartBody.Builder()
        bodyBuilder.addFormDataPart("test", apkFile.name, RequestBody.create(null, apkFile))
        val build = bodyBuilder.build()

//        val requestBody = ProgressHelper.withProgress(build, object : ProgressUIListener() {
//            //if you don't need this method, don't override this methd. It isn't an abstract method, just an empty method.
//            override fun onUIProgressStart(totalBytes: Long) {
//                super.onUIProgressStart(totalBytes)
//                Log.e("TAG", "onUIProgressStart:$totalBytes")
//            }
//
//            override fun onUIProgressChanged(numBytes: Long, totalBytes: Long, percent: Float, speed: Float) {
//                Log.e("TAG", "=============start===============")
//                Log.e("TAG", "numBytes:$numBytes")
//                Log.e("TAG", "totalBytes:$totalBytes")
//                Log.e("TAG", "percent:$percent")
//                Log.e("TAG", "speed:$speed")
//                Log.e("TAG", "============= end ===============")
//
//            }
//
//            //if you don't need this method, don't override this methd. It isn't an abstract method, just an empty method.
//            override fun onUIProgressFinish() {
//                super.onUIProgressFinish()
//                Log.e("TAG", "onUIProgressFinish:")
//
//            }
//        })
//        builder.post(requestBody)
        val call = okHttpClient.newCall(builder.build())
        call.enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                Log.e("TAG", "=============onFailure===============")
                e.printStackTrace()
            }

            @Throws(IOException::class)
            override fun onResponse(call: Call, response: Response) {
                Log.e("TAG", "=============onResponse===============")
//                Log.e("TAG", "request headers:" + response.request().headers())
//                Log.e("TAG", "response headers:" + response.headers())
            }
        })
    }

    private fun download() {
        val savePath = requireActivity().getExternalFilesDir("apk")!!.path
        val url = "https://dnkj-world-farm-prd.oss-accelerate.aliyuncs.com/data/fc-bee/app/ChaseFlower_M.apk"
        val okHttpClient = OkHttpClient()
        val builder = Request.Builder()
        builder.url(url)
        builder.get()
        val call = okHttpClient.newCall(builder.build())
//        call.enqueue(object : Callback {
//            override fun onFailure(call: Call, e: IOException) {
//                Log.e("TAG", "=============onFailure===============")
//                e.printStackTrace()
//            }
//
//            @Throws(IOException::class)
//            override fun onResponse(call: Call, response: Response) {
//                Log.e("TAG", "=============onResponse===============")
//                Log.e("TAG", "request headers:" + response.request().headers())
//                Log.e("TAG", "response headers:" + response.headers())
//                val responseBody = ProgressHelper.withProgress(response.body(), object : ProgressUIListener() {
//                    //if you don't need this method, don't override this methd. It isn't an abstract method, just an empty method.
//                    override fun onUIProgressStart(totalBytes: Long) {
//                        super.onUIProgressStart(totalBytes)
//                        Log.e("TAG", "onUIProgressStart:$totalBytes")
//                    }
//
//                    override fun onUIProgressChanged(numBytes: Long, totalBytes: Long, percent: Float, speed: Float) {
//                        Log.e("TAG", "=============start===============")
//                        Log.e("TAG", "numBytes:$numBytes")
//                        Log.e("TAG", "totalBytes:$totalBytes")
//                        Log.e("TAG", "percent:$percent")
//                        Log.e("TAG", "speed:$speed")
//                        Log.e("TAG", "============= end ===============")
//
//                    }
//
//                    //if you don't need this method, don't override this methd. It isn't an abstract method, just an empty method.
//                    override fun onUIProgressFinish() {
//                        super.onUIProgressFinish()
//                        Log.e("TAG", "onUIProgressFinish:")
//                    }
//                })
//                val source = responseBody.source()
//                val outFile = File(savePath,"do1.apk")
//                outFile.delete()
//                outFile.createNewFile()
//                val sink = Okio.buffer(Okio.sink(outFile))
//                source.readAll(sink)
//                sink.flush()
//                source.close()
//    }
//})
    }

}